var Base = {
	ctxPath : "",
	adminPath : "",
	addCtx : function(ctx) {
		if (this.ctxPath == "") {
			this.ctxPath = ctx;
		}
	},
	addAdmin : function(path) {
		if (this.adminPath == "") {
			this.adminPath = path;
		}
	},
	confirm : function(tip, ensure) {// 询问框
		parent.layer.confirm(tip, {
			btn : [ '确定', '取消' ]
		}, function(index) {
			ensure();
			parent.layer.close(index);
		}, function(index) {
			parent.layer.close(index);
		});
	},
	log : function(info) {
		console.log(info);
	},
	alert : function(info, iconIndex) {
		parent.layer.msg(info, {
			icon : iconIndex
		});
	},
	info : function(info) {
		Base.alert(info, 0);
	},
	success : function(info) {
		Base.alert(info, 1);
	},
	error : function(info) {
		Base.alert(info, 2);
	},
	infoDetail : function(title, info) {
		var display = "";
		if (typeof info == "string") {
			display = info;
		} else {
			if (info instanceof Array) {
				for ( var x in info) {
					display = display + info[x] + "<br/>";
				}
			} else {
				display = info;
			}
		}
		parent.layer.open({
			title : title,
			type : 1,
			skin : 'layui-layer-rim', // 加上边框
			area : [ '950px', '600px' ], // 宽高
			content : '<div style="padding: 20px;">' + display + '</div>'
		});
	},
	writeObj : function(obj) {
		var description = "";
		for ( var i in obj) {
			var property = obj[i];
			description += i + " = " + property + ",";
		}
		layer.alert(description, {
			skin : 'layui-layer-molv',
			closeBtn : 0
		});
	},
	showInputTree : function(inputId, inputTreeContentId, leftOffset,
			rightOffset) {
		var onBodyDown = function(event) {
			if (!(event.target.id == "menuBtn"
					|| event.target.id == inputTreeContentId || $(event.target)
					.parents("#" + inputTreeContentId).length > 0)) {
				$("#" + inputTreeContentId).fadeOut("fast");
				$("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
			}
		};

		if (leftOffset == undefined && rightOffset == undefined) {
			var inputDiv = $("#" + inputId);
			var inputDivOffset = $("#" + inputId).offset();
			$("#" + inputTreeContentId).css({
				left : inputDivOffset.left + "px",
				top : inputDivOffset.top + inputDiv.outerHeight() + "px"
			}).slideDown("fast");
		} else {
			$("#" + inputTreeContentId).css({
				left : leftOffset + "px",
				top : rightOffset + "px"
			}).slideDown("fast");
		}

		$("body").bind("mousedown", onBodyDown);
	},
	baseAjax : function(url, tip) {
		var ajax = new $ajax(Base.ctxPath + url, function(data) {
			Base.success(tip + "成功!");
		}, function(data) {
			Base.error(tip + "失败!" + data.responseJSON.message + "!");
		});
		return ajax;
	},
	changeAjax : function(url) {
		return Base.baseAjax(url, "修改");
	},
	zTreeCheckedNodes : function(zTreeId) {
		var zTree = $.fn.zTree.getZTreeObj(zTreeId);
		var nodes = zTree.getCheckedNodes();
		var ids = "";
		for (var i = 0, l = nodes.length; i < l; i++) {
			ids += "," + nodes[i].id;
		}
		return ids.substring(1);
	},
	eventParseObject : function(event) {// 获取点击事件的源对象
		event = event ? event : window.event;
		var obj = event.srcElement ? event.srcElement : event.target;
		return $(obj);
	},
	sessionTimeoutRegistry : function() {
		$.ajaxSetup({
			contentType : "application/x-www-form-urlencoded;charset=utf-8",
			complete : function(XMLHttpRequest, textStatus) {
				// 通过XMLHttpRequest取得响应头，sessionstatus，
				var sessionstatus = XMLHttpRequest
						.getResponseHeader("sessionstatus");
				if (sessionstatus == "timeout") {
					// 如果超时就处理 ，指定要跳转的页面
					window.location = Base.ctxPath + "/global/sessionError";
				}
			}
		});
	},
	initValidator : function(formId, fields) {
		$('#' + formId).bootstrapValidator({
			feedbackIcons : {
				valid : 'glyphicon glyphicon-ok',
				invalid : 'glyphicon glyphicon-remove',
				validating : 'glyphicon glyphicon-refresh'
			},
			fields : fields,
			live : 'enabled',
			message : '该字段不能为空'
		});
	},
	dateFomat : function(date,fmt) {
		if(!(date instanceof Date)){
			Base.error("非时间类型");
			return ;
		}
		if(!fmt){
			fmt = "yyyy-MM-dd hh:mm:ss";
		}
		var o = {   
			"M+" : date.getMonth()+1,                 //月份   
			"d+" : date.getDate(),                    //日   
			"h+" : date.getHours(),                   //小时   
			"m+" : date.getMinutes(),                 //分   
			"s+" : date.getSeconds(),                 //秒   
			"q+" : Math.floor((date.getMonth()+3)/3), //季度   
			"S"  : date.getMilliseconds()             //毫秒   
		};   
		if(/(y+)/.test(fmt))   
			fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
		for(var k in o)   
			if(new RegExp("("+ k +")").test(fmt))   
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
		return fmt;  
	}
};
